import 'package:fluro/fluro.dart';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

import '../../res/resources.dart';
import '../../routers/navigators.dart';
import '../../routers/routers.dart';
import '../../utils/toast.dart';
import '../../widget/app_bar.dart';
import '../../widget/my_button.dart';
import '../../widget/text_field.dart';
import 'login_router.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _phoneController = TextEditingController();
  TextEditingController _vCodeController = TextEditingController();

  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  bool _isClick = false;

  @override
  void initState() {
    super.initState();
  }

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.IOS,
      keyboardBarColor: Colors.grey[200],
      nextFocus: true,
      actions: [
        KeyboardAction(
          focusNode: _nodeText1,
          closeWidget: Padding(
            padding: EdgeInsets.all(5.0),
            child: Text("关闭"),
          ),
        ),
        KeyboardAction(
          focusNode: _nodeText2,
          closeWidget: Padding(
            padding: EdgeInsets.all(5.0),
            child: Text("关闭"),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        isBack: false,
        actionName: "Language: 简体中文",
        onPressed: () {
          _toggleLanguage();
        },
      ),
      body: defaultTargetPlatform == TargetPlatform.iOS
          ? FormKeyboardActions(
              child: _buildBody(),
            )
          : SingleChildScrollView(
              child: _buildBody(),
            ),
    );
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.only(left: 20, right: 20, top: 20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            "免密码登录",
            style: TextStyles.textBoldDark26,
          ),
          Gaps.vGap16,
          MyTextField(
            focusNode: _nodeText1,
            controller: _phoneController,
            maxLength: 11,
            keyboardType: TextInputType.phone,
            hintText: "请输入手机号码",
          ),
          Gaps.vGap8,
          MyTextField(
            focusNode: _nodeText2,
            config: _buildConfig(context),
            controller: _vCodeController,
            maxLength: 6,
            hintText: "请输入验证码",
            getVCode: () {
              Toast.show('获取验证码');
            },
          ),
          Gaps.vGap10,
          Gaps.vGap15,
          MyButton(
            onPressed: _login,
            text: "登录",
          ),
          Container(
            height: 45.0,
            alignment: Alignment.center,
            child: InkWell(
              highlightColor: Colors.transparent,
              splashColor: Colors.transparent,
              child: Text(
                '账号密码登陆',
                style: TextStyles.textState14,
              ),
              onTap: () {
                _toAccountLoginPage();
              },
            ),
          ),
          Container(
              alignment: Alignment.center,
              child: InkWell(
                highlightColor: Colors.transparent,
                splashColor: Colors.transparent,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                      '登陆即代表同意',
                      style: TextStyles.textGray14,
                    ),
                    Text(
                      '《用户协议》',
                      style: TextStyles.textState14,
                    )
                  ],
                ),
                onTap: () {
                  _toAgreementPage();
                },
              ))
        ],
      ),
    );
  }

  void _toggleLanguage() {
    Toast.show("切换语言");
  }

  void _login() {
    FlutterStars.SpUtil.putString("mobile", _phoneController.text);
    Navigators.push(context, Routes.home, clearStack: true);
  }

  void _toAccountLoginPage() {
    Navigators.push(context, LoginRouter.accountLoginPage,
        replace: true, transition: TransitionType.inFromRight);
  }

  void _toAgreementPage() {
    String url =
        "http://cdn.lifesense.com/sportsAppWebViews/webpack/simple_page/agreement.html";
    Navigators.goWebViewPage(context, "用户协议", url);
  }
}
